package com.mine.modules.vm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mine.modules.vm.common.utils.MonitorUtil;
import com.mine.common.utils.UuidUtil;
import com.mine.modules.vm.entity.VmMonitorRegions;
import com.mine.modules.vm.mapper.VmMonitorRegionsMapper;
import com.mine.modules.vm.service.IVmMonitorRegionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 监控设备区域表 服务实现类
 * </p>
 *
 * @author xujf
 * @since 2024-09-22
 */
@Slf4j
@Service
public class VmMonitorRegionsServiceImpl extends ServiceImpl<VmMonitorRegionsMapper, VmMonitorRegions> implements IVmMonitorRegionsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void synRegions() {
        try {
            boolean isEnd = false;
            int pageNo = 0;
            int total = 0;
            long start = System.currentTimeMillis();
            while (!isEnd) {
                pageNo++;
                int pageSize = 1000;
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("pageNo", pageNo);
                params.put("pageSize", pageSize);
                String regions = MonitorUtil.regions(params);
                if (StringUtils.isNotEmpty(regions)) {
                    JSONObject object = JSONObject.parseObject(regions);
                    if (object.getString("code").equals("0")) {
                        JSONObject data = JSONObject.parseObject(object.getString("data"));
                        if (data != null) {
                            total = data.getIntValue("total");
                            JSONArray list = JSONObject.parseArray(data.getString("list"));
                            if (list != null && !list.isEmpty()) {
                                List<String> indexCodeList = new ArrayList<String>();
                                for (Object obj : list) {
                                    JSONObject result = JSONObject.parseObject(obj.toString());
                                    indexCodeList.add(result.getString("indexCode"));
                                    String name = result.getString("name");
                                    String indexCode = result.getString("indexCode");
                                    String treeCode = result.getString("treeCode");
                                    String parentIndexCode = result.getString("parentIndexCode");

                                    if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(indexCode) && StringUtils.isNotEmpty(treeCode)
                                            && StringUtils.isNotEmpty(parentIndexCode)) {
                                        VmMonitorRegions vmMonitorRegions = lambdaQuery().eq(VmMonitorRegions::getIndexCode, indexCode).one();
                                        if (vmMonitorRegions == null) {
                                            vmMonitorRegions = new VmMonitorRegions();
                                            vmMonitorRegions.setIndexCode(indexCode);
                                            this.save(vmMonitorRegions);
                                        }
                                        vmMonitorRegions.setName(name);
                                        vmMonitorRegions.setParentIndexCode(parentIndexCode);
                                        vmMonitorRegions.setTreeCode(treeCode);
                                        this.updateById(vmMonitorRegions);
                                    }
                                }
                                //删除数据
                                LambdaQueryWrapper<VmMonitorRegions> srcQuery = new LambdaQueryWrapper<VmMonitorRegions>();
                                srcQuery.notIn(VmMonitorRegions::getIndexCode, indexCodeList);
                                this.remove(srcQuery);
                            } else {
                                isEnd = true;
                            }
                        } else {
                            isEnd = true;
                            log.error("分页获取区域列表接口无数据返回");
                        }
                    } else {
                        isEnd = true;
                        log.error("分页获取区域列表接口无数据返回");
                    }
                } else {
                    isEnd = true;
                    log.error("分页获取区域列表接口无数据返回");
                }
            }
            log.info("======同步【海康分页获取区域列表接口】=====数据量:【{}】条;耗时：【{}】毫秒", total, System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.error("分页获取区域列表异常【{}】", e.getMessage());
        }
    }
}
